(function(){"use strict";BX.namespace("BX.Filter");BX.Filter.Search=function(t){this.parent=null;this.container=null;this.input=null;this.preset=null;this.buttonsContainer=null;this.delay=800;this.timeout=null;this.init(t)};BX.Filter.Search.prototype={init:function(t){this.parent=t;BX.bind(this.getInput(),"input",BX.delegate(this._onInputWithoutDebounce,this));if(this.parent.getParam("ENABLE_LIVE_SEARCH")){BX.bind(this.getInput(),"input",BX.debounce(this._onInput,this.delay,this))}BX.bind(this.getInput(),"keydown",BX.delegate(this._onKeyDown,this));BX.bind(this.getFindButton(),"click",BX.delegate(this._onSearchClick,this));BX.bind(this.getContainer(),"click",BX.delegate(this._onSearchContainerClick,this));this.removeAutofocus();this.firstInit=true},removeAutofocus:function(){var t=this.getInput();if(!!t){t.blur();t.autofocus=null}},getFindButton:function(){if(!BX.type.isDomNode(this.findButton)){this.findButton=BX.Filter.Utils.getByClass(this.getContainer(),this.parent.settings.classSearchButton)}return this.findButton},_onSearchClick:function(){this.apply()},selectSquare:function(t){!!t&&BX.addClass(t,this.parent.settings.classSquareSelected)},selectSquares:function(){this.getSquares().forEach(this.selectSquare,this)},unselectSquare:function(t){!!t&&BX.removeClass(t,this.parent.settings.classSquareSelected)},unselectSquares:function(){this.getSquares().forEach(this.unselectSquare,this)},removeSquares:function(){this.getSquares().forEach(this.removeSquare,this)},isSquaresSelected:function(){var t=this.getSquares();return t.length&&t.every(this.isSquareSelected,this)},isSquareSelected:function(t){return!!t&&BX.hasClass(t,this.parent.settings.classSquareSelected)},getLastSquare:function(){var t=this.getSquares();return!!t?t[t.length-1]:null},isTextSelected:function(){var t=this.getSearchString().length;var e=this.getInput();var s=e.selectionStart;var i=e.selectionEnd;return s===0&&i!==0&&i===t},isSelectionStart:function(){var t=this.getInput();var e=t.selectionStart;var s=t.selectionEnd;return e===0&&s===0},isSquareRemoveButton:function(t){return!!t&&BX.hasClass(t,this.parent.settings.classSquareDelete)},isClearButton:function(t){return!!t&&BX.hasClass(t,this.parent.settings.classClearSearchValueButton)},getClearButton:function(){return this.getContainer().querySelector("."+this.parent.settings.classClearSearchValueButton)},isSearchButton:function(t){return!!t&&BX.hasClass(t,this.parent.settings.classSearchButton)},adjustFocus:function(){if(!BX.browser.IsMobile()){var t=this.getInput();if(document.activeElement!==t&&window.scrollY<BX.pos(t).top){t.value=t.value;t.blur();t.focus()}}},findSquareByChild:function(t){return BX.findParent(t,{className:this.parent.settings.classSquare},true,false)},getSquareData:function(t){var e=BX.data(t,"item");return!!t&&!!e?JSON.parse(e):null},isSquareControl:function(t){var e=this.getSquareData(t);return!!e&&(e.type==="control"||BX.type.isArray(e))},onPresetSquareRemove:function(){var t=this.parent;var e=t.getPreset();var s=e.getCurrentPresetId();var i=t.getParam("RESET_TO_DEFAULT_MODE");var r=t.getParam("VALUE_REQUIRED");var a=e.isPinned(s);var n=this.getSquares();if(n.length===1){if(r&&a){this.parent.showPopup();this.adjustPlaceholder();this.parent.getPreset().deactivateAllPresets()}else{if(i&&a||!i){var o=true;this.lastPromise=t.resetFilter(o);t.closePopup()}}if(i&&!a){this.lastPromise=t.getPreset().applyPinnedPreset()}}if(n.length>1){var h=e.getPreset(e.getCurrentPresetId());var l=e.getPreset("tmp_filter");l.FIELDS=BX.clone(h.ADDITIONAL);h.ADDITIONAL=[];e.deactivateAllPresets();e.applyPreset("tmp_filter");t.applyFilter()}},onControlSquareRemove:function(t){var e=this.parent;var s=e.getPreset();var i=e.getParam("RESET_TO_DEFAULT_MODE");var r=e.getParam("VALUE_REQUIRED");var a;if(i&&this.getSquares().length===1){if(r){a=this.getSquareData(t);e.clearControls(a);this.parent.showPopup();this.adjustPlaceholder();this.parent.getPreset().deactivateAllPresets()}else{this.lastPromise=e.getPreset().applyPinnedPreset()}}else{a=this.getSquareData(t);e.clearControls(a);e.closePopup();if(BX.type.isArray(a)){a.forEach(function(t){s.removeAdditionalField(t.name)})}if(BX.type.isPlainObject(a)){s.removeAdditionalField(a.name)}this.apply()}},onValueRequiredSquareRemove:function(){var t=this.parent;t.getPreset().deactivateAllPresets();t.showPopup();this.adjustPlaceholder()},complexSquareRemove:function(t){var e=this.parent.getParam("VALUE_REQUIRED_MODE");var s=!this.isSquareControl(t);if(e){this.onValueRequiredSquareRemove()}else{if(s){this.onPresetSquareRemove()}else{this.onControlSquareRemove(t)}}this.removeSquare(t);this.adjustClearButton()},adjustClearButton:function(){!!this.getLastSquare()?this.showClearButton():this.hideClearButton()},removeSquare:function(t){!!t&&BX.remove(t)},_onSearchContainerClick:function(t){var e=this.parent;if(this.isClearButton(t.target)){if(!e.getParam("VALUE_REQUIRED")){if(!e.getParam("VALUE_REQUIRED_MODE")){if(e.getParam("RESET_TO_DEFAULT_MODE")){this.clearInput();this.lastPromise=e.getPreset().applyPinnedPreset()}else{e.resetFilter()}e.closePopup();this.adjustFocus()}else{this.removeSquares();e.showPopup();this.adjustPlaceholder();this.hideClearButton();e.getPreset().deactivateAllPresets()}}else{var s=e.getPreset().isPinned(e.getPreset().getCurrentPresetId());if(s||e.getPreset().getCurrentPresetId()==="tmp_filter"){var i=e.getPreset().getPreset(e.getPreset().getCurrentPresetId());if(i.ADDITIONAL.length){i.ADDITIONAL=[];this.lastPromise=e.getPreset().applyPreset(e.getPreset().getCurrentPresetId());this.apply()}else{this.removeSquares();e.showPopup();this.adjustPlaceholder();this.hideClearButton();e.getPreset().deactivateAllPresets()}}else{if(e.getParam("RESET_TO_DEFAULT_MODE")){this.lastPromise=e.getPreset().applyPinnedPreset()}else{e.resetFilter()}e.closePopup();this.adjustFocus()}this.clearInput()}}else if(this.isSearchButton(t.target)){this.apply();this.adjustFocus()}else if(this.isSquareRemoveButton(t.target)){var r=this.findSquareByChild(t.target);this.complexSquareRemove(r);this.adjustFocus()}else{if(!e.getPopup().isShown()){e.showPopup()}else{var a=this.getInput();var n=a.selectionStart;var o=a.selectionEnd;var h=this.getSearchString().length;if(!(h&&n===0&&o===h)){if(e.getParam("VALUE_REQUIRED")){if(!this.getSquares().length){this.lastPromise=e.getPreset().applyPinnedPreset()}else{e.closePopup()}}else{e.closePopup();if(e.getParam("VALUE_REQUIRED_MODE")){e.restoreRemovedPreset()}}}}}},_onKeyDown:function(t){var e=BX.Filter.Utils;var s=this.parent;if(e.isKey(t,"enter")){if(s.getParam("VALUE_REQUIRED")){if(!this.getSquares().length){this.parent.getPreset().applyPinnedPreset()}else{this.apply();this.firstInit=false;this.lastSearchString=this.getSearchString()}}else{this.apply();this.firstInit=false;this.lastSearchString=this.getSearchString()}s.closePopup()}if(e.isKey(t,"tab")||e.isKey(t,"downArrow")){s.showPopup();s.adjustFocus();this.unselectSquares()}if(e.isKey(t,"upArrow")){s.closePopup();if(s.getParam("VALUE_REQUIRED_MODE")){this.parent.restoreRemovedPreset()}if(s.getParam("VALUE_REQUIRED")){if(!this.getSquares().length){this.parent.getPreset().applyPinnedPreset()}}}if(e.isKey(t,"a")&&t.metaKey||e.isKey(t,"a")&&t.ctrlKey){this.selectSquares()}if(e.isKey(t,"backspace")&&this.isTextSelected()&&this.isSquaresSelected()){clearTimeout(this.timeout);if(this.parent.getParam("VALUE_REQUIRED")){var i=this.parent.getPreset().isPinned(this.parent.getPreset().getCurrentPresetId());if(i){this.removeSquares();this.parent.showPopup();this.adjustPlaceholder();this.hideClearButton();this.parent.getPreset().deactivateAllPresets()}else{if(this.parent.getParam("RESET_TO_DEFAULT_MODE")){this.lastPromise=this.parent.getPreset().applyPinnedPreset()}else{this.parent.resetFilter()}this.parent.closePopup();this.adjustFocus()}this.clearInput()}else{if(this.parent.getParam("RESET_TO_DEFAULT_MODE")){this.lastPromise=this.parent.getPreset().applyPinnedPreset()}else{this.lastPromise=this.parent.resetFilter()}this.parent.closePopup()}}if(e.isKey(t,"backspace")&&this.isSelectionStart()){clearTimeout(this.timeout);var r=this.getLastSquare();this.isSquareSelected(r)?this.complexSquareRemove(r):this.selectSquare(r)}if(!e.isKey(t,"backspace")&&!t.metaKey&&this.isSquaresSelected()){this.unselectSquares()}},getSearchString:function(){var t=this.getInput();return!!t?t.value:""},getSquares:function(){return BX.Filter.Utils.getByClass(this.getContainer(),this.parent.settings.classSquare,true)},adjustPlaceholder:function(){if(this.parent.getParam("LIMITS_ENABLED")){this.setInputPlaceholder(this.parent.getParam("MAIN_UI_FILTER__PLACEHOLDER_LIMITS_EXCEEDED"))}else if(this.parent.getParam("DISABLE_SEARCH")||!this.parent.settings.get("SEARCH")){this.setInputPlaceholder(this.parent.getParam("MAIN_UI_FILTER__PLACEHOLDER"))}else{this.setInputPlaceholder(this.parent.getParam("MAIN_UI_FILTER__PLACEHOLDER_DEFAULT"))}},isResolvedRequest:function(){return!this.lastPromise||!!this.lastPromise&&this.lastPromise.state},apply:function(){if(this.isResolvedRequest()){this.lastPromise=this.parent._onFindButtonClick()}return this.lastPromise},reset:function(){if(this.isResolvedRequest()){this.parent.getSearch().removePreset();this.parent.getPreset().deactivateAllPresets();this.parent.getPreset().resetPreset(true);this.timeout=setTimeout(BX.delegate(function(){this.lastPromise=this.parent.resetFilter()},this),this.delay)}return this.lastPromise},_onInputWithoutDebounce:function(){clearTimeout(this.timeout);var t=this.getSearchString();this.lastSearchString=!!this.lastSearchString?this.lastSearchString:t;if(t!==this.lastSearchString&&(!this.parent.isIe()||!this.firstInit)){if(this.parent.getParam("ENABLE_LIVE_SEARCH")){this.parent.showGridAnimation();BX.onCustomEvent(window,"BX.Filter.Search:input",[this.parent.params.FILTER_ID,t])}this.parent.getPopup().isShown()&&this.parent.closePopup()}if(t){this.showClearButton()}else{if(!this.getSquares().length&&this.lastSearchString!==t){this.hideClearButton();this.adjustPlaceholder()}}},_onInput:function(){var t=this.getSearchString();if(t!==this.lastSearchString&&(!this.parent.isIe()||!this.firstInit)){this.apply()}this.firstInit=false;this.lastSearchString=t},getButtonsContainer:function(){if(!BX.type.isDomNode(this.buttonsContainer)){this.buttonsContainer=BX.Filter.Utils.getByClass(this.getContainer(),this.parent.settings.classSearchButtonsContainer)}return this.buttonsContainer},showClearButton:function(){BX.addClass(this.getButtonsContainer(),this.parent.settings.classShow)},hideClearButton:function(){BX.removeClass(this.getButtonsContainer(),this.parent.settings.classShow)},getInput:function(){var t;if(!BX.type.isDomNode(this.input)){t=[this.parent.getParam("FILTER_ID",""),"_search"].join("");this.input=BX(t)}return this.input},getContainer:function(){var t;if(!BX.type.isDomNode(this.container)){t=[this.parent.getParam("FILTER_ID"),"_search_container"].join("");this.container=BX(t)}return this.container},setInputPlaceholder:function(t){var e=this.getInput();e.placeholder=t},clearInput:function(){var t=this.getInput();if(BX.type.isDomNode(t)){t.value=null}},clearForm:function(){this.clearInput();this.removePreset()},makeSquares:function(t,e,s){var i;var r=null;var a=this.getContainer();var n={squares:[],moreSquares:[]};t.forEach(function(t,o){if(o<e){i=BX.decl(t);r=r||i;if(!s){if(o===0){BX.prepend(i,a)}else{BX.insertAfter(i,r)}}else{var h=BX.Filter.Utils.getByClass(this.getContainer(),this.parent.settings.classSquare);if(h){BX.insertAfter(i,h)}else{BX.prepend(i,a)}}r=i;n.squares.push(i)}else{n.moreSquares.push({type:"control",name:t.value,title:t.title})}},this);return n},squares:function(t,e,s){var i,r,a,n,o;var h=BX.Filter.Utils.getByClass(this.getContainer(),this.parent.settings.classSquare,true);if(s){h.forEach(function(t){var e=BX.data(t,"item");if(e){BX.remove(t)}})}else{h.forEach(BX.remove)}i=this.prepareSquaresData(t);r=this.makeSquares(i,e,s);n=0;o={squaresData:i,width:0};if(r.moreSquares.length){a={block:"main-ui-search-square",name:this.parent.getParam("MAIN_UI_FILTER__AND")+" "+this.parent.getParam("MAIN_UI_FILTER__MORE")+" "+r.moreSquares.length,item:r.moreSquares,title:r.moreSquares.map(function(t){return t.title}).join(", \n")};a=BX.decl(a);r.squares.push(a);BX.insertAfter(a,r.squares[r.squares.length-2]);n=r.squares.reduce(function(t,e){return t+BX.width(e)+(parseFloat(BX.style(e,"margin-right"))||0)},0)}o.width=n;return o},setPreset:function(t){var e=this.getContainer();var s,i;var r;if(BX.type.isPlainObject(t)){i=BX.Filter.Utils.getByClass(e,this.parent.settings.classSquare,true);i.forEach(BX.remove);t=BX.clone(t);t.ADDITIONAL=t.ADDITIONAL||[];BX.onCustomEvent(window,"BX.Filter.Search:beforeSquaresUpdate",[t,this]);if(t.ID!=="default_filter"&&t.ID!=="tmp_filter"){s=BX.decl({block:"main-ui-search-square",name:t.TITLE,value:t.ID,isPreset:true});BX.prepend(s,e);if("ADDITIONAL"in t&&BX.type.isArray(t.ADDITIONAL)&&t.ADDITIONAL.length){r=this.squares(t.ADDITIONAL,1,true);if(BX.width(e)-r.width<100){r=this.squares(t.ADDITIONAL,0,true)}}}else{if("ADDITIONAL"in t&&BX.type.isArray(t.ADDITIONAL)&&t.ADDITIONAL.length){t.ADDITIONAL.forEach(function(e,s){if(!("ID"in e)){e.ID="ADDITIONAL_ID_"+s}if(!("NAME"in e)){e.NAME="ADDITIONAL_NAME_"+s}if(!("TYPE"in e)){e.TYPE="STRING"}if("LABEL"in e&&"LABEL"in e){t.FIELDS.push(e)}})}if(BX.type.isArray(t.FIELDS)&&t.FIELDS.length){r=this.squares(t.FIELDS,2);if(BX.width(e)-r.width<100){r=this.squares(t.FIELDS,1)}}}if(r&&BX.type.isArray(r.squaresData)&&r.squaresData.length||t.ID!=="default_filter"&&t.ID!=="tmp_filter"){if(this.parent.getParam("LIMITS_ENABLED")){this.setInputPlaceholder(this.parent.getParam("MAIN_UI_FILTER__PLACEHOLDER_LIMITS_EXCEEDED"))}else{this.setInputPlaceholder(this.parent.getParam("MAIN_UI_FILTER__PLACEHOLDER_WITH_FILTER"))}this.showClearButton()}else{this.adjustPlaceholder()}if(BX.type.isNotEmptyString(this.parent.getSearch().getInput().value)){this.showClearButton()}}},prepareSquaresData:function(t){var e,s,i,r;var a=[];t=t.filter(function(t){return!!t});t.map(function(t){e=null;switch(t.TYPE){case this.parent.types.DATE:{e=t.LABEL+": "+t.SUB_TYPE.NAME;if(t.SUB_TYPE.VALUE===this.parent.dateTypes.QUARTER&&BX.type.isNotEmptyString(t.VALUES._quarter)){var r=t.QUARTERS.filter(function(e){return e.VALUE==t.VALUES._quarter}).map(function(t){return t.NAME});r=r.length?r.join(""):"";e=t.LABEL+": "+r+" "+this.parent.getParam("MAIN_UI_FILTER__QUARTER").toLocaleLowerCase()+" "+t.VALUES._year}if(t.SUB_TYPE.VALUE===this.parent.dateTypes.YEAR&&BX.type.isNotEmptyString(t.VALUES._year)){e=t.LABEL+": "+t.VALUES._year}if(t.SUB_TYPE.VALUE===this.parent.dateTypes.MONTH&&BX.type.isNotEmptyString(t.VALUES._month)){var n=t.MONTHS.filter(function(e){return e.VALUE==t.VALUES._month}).map(function(t){return t.NAME});n=n.length?n.join(""):"";e=t.LABEL+": "+n+" "+t.VALUES._year}if(t.SUB_TYPE.VALUE===this.parent.dateTypes.EXACT&&BX.type.isNotEmptyString(t.VALUES._from)){e=t.LABEL+": "+t.VALUES._from}if(t.SUB_TYPE.VALUE===this.parent.dateTypes.RANGE){if(BX.type.isNotEmptyString(t.VALUES._from)&&BX.type.isNotEmptyString(t.VALUES._to)){e=t.LABEL+": "+t.VALUES._from+"-"+t.VALUES._to}else if(!BX.type.isNotEmptyString(t.VALUES._from)&&BX.type.isNotEmptyString(t.VALUES._to)){e=t.LABEL+": "+this.parent.getParam("MAIN_UI_FILTER__BEFORE")+" "+t.VALUES._to}else if(BX.type.isNotEmptyString(t.VALUES._from)&&!BX.type.isNotEmptyString(t.VALUES._to)){e=t.LABEL+": "+this.parent.getParam("MAIN_UI_FILTER__AFTER")+" "+t.VALUES._from}}if((t.SUB_TYPE.VALUE===this.parent.dateTypes.NEXT_DAYS||t.SUB_TYPE.VALUE===this.parent.dateTypes.PREV_DAYS)&&!BX.type.isNumber(parseInt(t.VALUES._days))){e=null}if(t.SUB_TYPE.VALUE===this.parent.dateTypes.NEXT_DAYS&&BX.type.isNumber(parseInt(t.VALUES._days))){e=t.LABEL+": "+this.parent.getParam("MAIN_UI_FILTER__DATE_NEXT_DAYS_LABEL").replace("#N#",t.VALUES._days)}if(t.SUB_TYPE.VALUE===this.parent.dateTypes.PREV_DAYS&&BX.type.isNumber(parseInt(t.VALUES._days))){e=t.LABEL+": "+this.parent.getParam("MAIN_UI_FILTER__DATE_PREV_DAYS_LABEL").replace("#N#",t.VALUES._days)}if(t.SUB_TYPE.VALUE===this.parent.dateTypes.NONE){e=null}break}case this.parent.types.CUSTOM_DATE:{if(BX.type.isArray(t.VALUE.days)&&t.VALUE.days.length||BX.type.isArray(t.VALUE.months)&&t.VALUE.months.length||BX.type.isArray(t.VALUE.years)&&t.VALUE.years.length){e=t.LABEL}break}case this.parent.types.SELECT:{if(BX.type.isPlainObject(t.VALUE)&&t.VALUE.VALUE||t.STRICT){e=t.LABEL+": "+t.VALUE.NAME}break}case this.parent.types.MULTI_SELECT:{if(BX.type.isArray(t.VALUE)&&t.VALUE.length){s=[];e=t.LABEL+": ";t.VALUE.forEach(function(t,e){if(e<2){s.push(t.NAME)}});e+=s.join(", ");if(t.VALUE.length>2){i=[];t.VALUE.forEach(function(t){i.push(t.NAME)});e=i.join(", ")}}break}case this.parent.types.NUMBER:{if(t.SUB_TYPE.VALUE==="exact"){if(BX.type.isNotEmptyString(t.VALUES._from)){e=t.LABEL+": "+t.VALUES._from}else{e=null}}if(t.SUB_TYPE.VALUE==="range"){if(BX.type.isNotEmptyString(t.VALUES._from)&&BX.type.isNotEmptyString(t.VALUES._to)){e=t.LABEL+": "+t.VALUES._from+"-"+t.VALUES._to}else if(!BX.type.isNotEmptyString(t.VALUES._from)&&BX.type.isNotEmptyString(t.VALUES._to)){e=t.LABEL+": "+this.parent.getParam("MAIN_UI_FILTER__NUMBER_LESS")+" "+t.VALUES._to}else if(BX.type.isNotEmptyString(t.VALUES._from)&&!BX.type.isNotEmptyString(t.VALUES._to)){e=t.LABEL+": "+this.parent.getParam("MAIN_UI_FILTER__NUMBER_MORE")+" "+t.VALUES._from}else{e=null}}if(t.SUB_TYPE.VALUE==="more"){if(BX.type.isNotEmptyString(t.VALUES._from)){e=t.LABEL+": > ";e+=t.VALUES._from}}if(t.SUB_TYPE.VALUE==="less"){if(BX.type.isNotEmptyString(t.VALUES._to)){e=t.LABEL+": < ";e+=t.VALUES._to}}break}case this.parent.types.CUSTOM_ENTITY:case this.parent.types.DEST_SELECTOR:{if(t.MULTIPLE){var o=!!t.VALUES._label?t.VALUES._label:[];if(BX.type.isPlainObject(o)){o=Object.keys(o).map(function(t){return o[t]})}if(!BX.type.isArray(o)){o=[o]}if(o.length>0){e=t.LABEL+": ";e+=o.join(", ")}}else{if(BX.type.isNotEmptyString(t.VALUES._value)&&BX.type.isNotEmptyString(t.VALUES._label)){e=t.LABEL+": ";e+=t.VALUES._label}}break}case this.parent.types.CUSTOM:{e="_VALUE"in t&&BX.type.isNotEmptyString(t._VALUE)?t.LABEL:null;break}default:{if(BX.type.isNotEmptyString(t.VALUE)){e=t.LABEL+": "+t.VALUE}break}}if(e!==null){a.push({block:"main-ui-search-square",name:e,value:t.NAME,item:{type:"control",name:t.NAME},title:e})}},this);return a},getPreset:function(){var t=this.getContainer();var e=this.parent.settings.classSquare;var s=null;if(BX.type.isDomNode(t)){s=BX.Filter.Utils.getByClass(t,e)}return s},removePreset:function(){var t=this.getPreset();if(BX.type.isDomNode(t)){BX.remove(t);this.adjustPlaceholder()}this.hideClearButton()},updatePreset:function(t){this.removePreset();this.setPreset(t)}}})();
//# sourceMappingURL=search.map.js